import { Controller, Get, Headers, Res, Logger } from "@nestjs/common";
import { I18nRequestScopeService } from "nestjs-i18n";
import { ConfigService } from "../config/config.service";

@Controller()
export class MobileClientController {
    constructor(
        private readonly configService: ConfigService,
        private readonly i18n: I18nRequestScopeService,
        private readonly logger: Logger
    ) {}

    @Get()
    async get(@Headers("user-agent") userAgent, @Res() res) {
        if (!/mobile|ios|android|Qing|HuaweiBrowser/i.test(userAgent)) {
            res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <meta http-equiv="X-UA-Compatible" content="IE=11" />
                    <title>AISHU AnyShare 7</title>
                </head><body>
                    <p  style="font-size: 40px;text-align: center;margin-top: 55%;padding: 20px;color: #000;font-weight: 100;">
                    ${await this.i18n.translate(`${"MobileClient.OPEN_LINK"}`)}</p>
                    </body></html>`);
        } else {
            const IOS = /(ipad|iphone)/i.test(userAgent);
            const type: any = {};

            this.logger.log("{/deploy-manager/client/downloadable GET} start");
            const Client: any = await this.configService.deployServiceApi.get(
                "/deploy-manager/client/downloadable" as any
            );
            for (const value of Client.data) {
                type[value] = value;
            }
            this.logger.log("{/deploy-manager/client/downloadable GET} success");
            if (IOS) {
                try {
                    const [Ios, IosPackage]: any = await Promise.all([
                        this.configService.deployServiceApi.get("/deploy-manager/client/download-url" as any, {
                            query: {
                                os_type: "ios",
                                req_host: this.configService.publicInfo.hostname,
                            },
                        }),
                        this.configService.deployServiceApi.get("/deploy-manager/client/package" as any, {
                            query: {
                                os_type: "ios",
                            },
                        }),
                    ]);
                    if (type.ios) {
                        if (Ios.data.url) {
                            res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <title>${await this.i18n.translate(`${"MobileClient.VERSION_UPDATE"}`)}</title>
                </head><body>

                    <img style="width:144px;height:144px;display:block;margin:0 auto;margin-top:38%" src="" />
                   <p style="text-align:center;font-size: 38px;">${await this.i18n.translate(
                       `${"MobileClient.VERSION_UPDATE"}`
                   )}</p>
                   <p style="text-align:center;font-size: 38px;">${await this.i18n.translate(
                       `${"MobileClient.VERSION"}`
                   )}${IosPackage.data[0].version}</p>
                    <a style="font-size: 40px;border-radius:8px;text-align: center;color: #333;font-weight: 600;display:block;width:630px;margin:0 auto;margin-top: 5%;height:88px;line-height:88px;text-decoration:none;"  href="${
                        Ios.data.url
                    }">${await this.i18n.translate(`${"MobileClient.CLICK_JUMP"}`)}</a>
                    </body></html>`);
                        }
                    } else {
                        res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <title>${await this.i18n.translate(`${"MobileClient.VERSION_UPDATE"}`)}</title>
                </head>
                     <script>
                     alert('${await this.i18n.translate(`${"MobileClient.IOS_NOT_DOWNLOAD_LINK"}`)}')
                    window.close();
                    </script>
                    </html>`);
                    }
                } catch (error) {
                    res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <title>${await this.i18n.translate(`${"MobileClient.VERSION_UPDATE"}`)}</title>
                </head>
                     <script>
                     alert('${await this.i18n.translate(`${"MobileClient.MISSING_IOS_DOWNLOAD_LINK"}`)}')
                    window.close();
                    </script>

                    </html>`);
                }
            } else {
                try {
                    const [Android, AndroidPackage]: any = await Promise.all([
                        await this.configService.deployServiceApi.get("/deploy-manager/client/download-url" as any, {
                            query: {
                                os_type: "android",
                                req_host: this.configService.publicInfo.hostname,
                            },
                        }),
                        this.configService.deployServiceApi.get("/deploy-manager/client/package" as any, {
                            query: {
                                os_type: "android",
                            },
                        }),
                    ]);
                    if (type.android) {
                        if (Android.data.url) {
                            res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <title>${await this.i18n.translate(`${"MobileClient.VERSION_UPDATE"}`)}</title>
                </head><body>

                 <img style="width:144px;height:144px;display:block;margin:0 auto;margin-top:45%" src="" />
                   <p style="text-align:center;font-size: 38px;">${await this.i18n.translate(
                       `${"MobileClient.VERSION_UPDATE"}`
                   )}</p>
                   <p style="text-align:center;font-size: 38px;">${await this.i18n.translate(
                       `${"MobileClient.VERSION"}`
                   )}${AndroidPackage.data[0].version}</p>
                    <a  style="font-size: 40px;border-radius:8px;text-align: center;color: #333;font-weight: 100;display:block;width:630px;margin:0 auto;margin-top: 5%;height:88px;line-height:88px;text-decoration:none;" href="${
                        Android.data.url
                    }">${await this.i18n.translate(`${"MobileClient.CLICK_JUMP"}`)}</a>
                    </body></html>`);
                        }
                    } else {
                        res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <title>${await this.i18n.translate(`${"MobileClient.VERSION_UPDATE"}`)}</title>
                </head>
                    <script>
                     alert('${await this.i18n.translate(`${"MobileClient.ANDROID_NOT_DOWNLOAD_LINK"}`)}')
                    window.close();
                    </script>
                    </html>`);
                    }
                } catch (error) {
                    res.send(`<!DOCTYPE html><html lang="zh"><head>
                    <meta charset="UTF-8">
                    <title>${await this.i18n.translate(`${"MobileClient.VERSION_UPDATE"}`)}</title>
                </head>
                    <script>
                     alert('${await this.i18n.translate(`${"MobileClient.MISSING_ANDROID_DOWNLOAD_LINK"}`)}')
                    window.close();
                    </script>

                    </html>`);
                }
            }
        }
    }
}
